<?php

class projects_View extends View {
     
    public function createView($action, $params) {
        switch($action) {
            
            
            //PROJECTS MAIN MENU
            case 'menu': 
                $buttons = array();
                $buttons[] = array('action'=>'add', 'text'=>'Dodaj projekt');
                $buttons[] = array('action'=>'delete', 'text'=>'Usuń projekt');
                $buttons[] = array('action'=>'manage', 'text'=>'Zarządzaj istniejącym');
                $this->view = Menu::createMenu($buttons, $params, true, array(2,2),true);
            break;
        
        
            //ADD PROJECT
            case 'addError':
                $this->view = $this->errorInfo("Nie udało się stworzyć projektu.");
            case 'add':
                $project = isset($_POST['project']) ? $_POST['project'] : array();
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Dane projektu",'projectData');
                        $f->section()->add(new Input('Nazwa: ', 'text', "project[name]",isset($project['name']) ? $project['name'] : ''));
                        $textArea = new Textarea('project[description]', isset($project['description']) ? $project['description'] : '', '', 'width: 350px; height: 150px;', 20, 40);
                        $f->section()->add(new PureHtml('Opis:<br>' . $textArea->get()));
                        $f->section()->add(new Input('Priorytet:', 'text', 'project[priority]', isset($project['priority']) ? $project['priority'] : '','','','','priority'));
                        $f->section()->add(new Input('Data rozpoczęcia:', 'text', 'project[dateStart]', isset($project['dateStart']) ? $project['dateStart'] : date("Y-m-d"),'','','','dateStart'));
                        $f->section()->add(new Input('Data zakończenia:', 'text',  'project[dateEnd]', isset($project['dateEnd']) ? $project['dateEnd'] : date("Y-m-d"),'','','','dateEnd'));
                    $f->addButton(new Input('', 'submit', 'sbmt', 'Dodaj'));
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '\'"'));
                $this->view .= $f->get();
            break;
            case 'addSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'_default','text'=>'Powrót');
                $project = new Project((int)$params['pid']);
                unset($params['pid']);
                $this->view = 'Projekt "' . $project->name . '" został utworzony (id:' . $project->getPid() . ')';
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
        
            //DELETE PROJECT
            case 'deleteError':
                $this->view = $this->errorInfo("Nie udało się usunąć projektu.");
            case 'delete':
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Projekty",'projectDelete');
                      $f->section()->add(new Select("", ProjectDAO::getProjects(true), "pid", 0));
                    $f->addButton(new Input('', 'submit', 'sbmt', 'Usuń','','','onClick="return confirm(\'Czy na pewno chcesz usunąć ten projekt? Wiąże się to z usunięciem WSZYSTKICH danych z nim związanych.\');"'));
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '\'"'));
                $this->view .= $f->get();
            break;
            case 'deleteSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'_default','text'=>'Powrót');
                $project = $params['project'];
                unset($params['project']);
                $this->view = 'Projekt "' . $project->name . '" został usunięty.';
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
            //PROJECT LIST
            case 'manage':
                $projects = ProjectDAO::getProjects(false);
                $table = '<table class="projectsList"><thead><th>Projekt</th><th>Zadań</th><th>W trakcie realizacji</th></thead><tbody>';
                foreach($projects as $key => $project) {
                    $project = new Project((int)$project->pid);
                    $tasks = $project->getTasks();
                    $atProgress = 0;
                    foreach($tasks as $key => $task) {
                        if($task->atProgress()) {
                            $atProgress++;
                        }
                    }
                    $table .= '<tr class="project" pid="' . $project->getPid() . '"><td>' . $project->name . '</td><td>' . count($tasks) . '</td><td>' . $atProgress . '</td></tr>';
                }
                $table .= '</tbody></table>';
                $this->view = "<center><b>Wybierz projekt</b></center><br />" . $table . '<br />';
                $buttons = array();
                $buttons[] = array('action'=>'_default','text'=>'Powrót');
                $this->view .= Menu::createMenu($buttons, $params, false, array(), true);
            break;
            
            //MANAGE PROJECT
            case 'manageProject':
                $project = new Project((int)$params['pid']);
                $table = '<table class="projectInfo"><thead><tr><th colspan="2">' . $project->name . '<br />(priorytet: ' . $project->priority . ')</th></tr></thead><tbody>';
                $table .= '<tr><td>Opis</td><td><i>' . $project->description . '</i></td></tr>';
                $table .= '<tr><td>Utworzony</td><td>' . $project->getDateCreated() . '</td></tr>';
                $table .= '<tr><td>Data rozpoczęcia</td><td>' . ($project->dateStart ? $project->dateStart : 'brak' ). '</td></tr>';
                $table .= '<tr><td>Planowana data zakończenia</td><td>' . ($project->dateEnd ? $project->dateEnd : 'brak') . '</td></tr>';
                $table .= '</tbody></table><br />';
                $this->view .= $table;
                //dopisać info o projekcie w pierwszej kolejności ;)
                unset($params['pid']);
                if($project->getPid() < 1) {
                    $this->view .= '<center><b>Wybrany projekt nie istnieje.</b></center><br />';
                    $buttons = array();
                    $buttons[] = array('action'=>'manage','text'=>'Powrót');
                    $this->view .= Menu::createMenu($buttons, $params, false, array(), true);
                } else {
                    $tasks = $project->getTasks();
                    $table = '<table class="tasksList">
                                <thead>
                                    <tr>
                                        <th colspan="7">Zadania<input type="button" pid="' . $project->getPid() . '" id="addTask" value="Nowe" /></th>
                                    </tr>
                                    <tr>
                                        <th>Zadanie</th>
                                        <th>Użytkownik</th>
                                        <th>Data rozpoczęcia</th>
                                        <th>Przewidywana data zakończenia</th>
                                        <th>Ukończone</th>
                                        <th>W trakcie realizacji</th>
                                        <th>Akcje</th>
                                    </tr>
                                </thead>
                                <tbody>';
                    foreach($tasks as $key => $task) {
                        $user = new AnyUser((int)$task->getUser());
                        $user = (strlen($user->getName()) > 0 ? $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ')' . '<input type="button" value="Usuń" class="removeUser" />' : '<input type="button" value="Przypisz" class="addUser" />');
                        $done = ($task->getDone() > 0 ? $task->getDateDone() : ($task->atProgress() ? 'nie' : '<input type="button" value="Zakończ" class="endTask" />'));
                        $table .= '<tr ptid="' . $task->getPtid() . '" class="task">';
                            $table .='<td>' . $task->name . '</td>';
                            $table .='<td>' . $user . '</td>';
                            $table .='<td>' . ($task->dateStart ? $task->dateStart : 'brak') . '</td>';
                            $table .='<td>' . ($task->dateEnd ? $task->dateEnd : 'brak') . '</td>';
                            $table .='<td>' . $done . '</td>';
                            $table .='<td>' . ($task->atProgress() ? 'tak' : 'nie') . '</td>';
                            $table .='<td>' . ($task->getDone() ? '' : '<input type="button" value="Edytuj" class="editTask" />') . '<input type="button" value="Usuń" class="removeTask" /></td>';
                        $table .= '</tr>';
                    }
                    $table .= '</tbody><tfoot><tr><td colspan="7">Łącznie: ' . count($tasks) . '</td></tr></tfoot></table><br />';
                    $this->view .= $table;
                    $buttons = array();
                    $buttons[] = array('action'=>'manage','text'=>'Powrót');
                    $this->view .= Menu::createMenu($buttons, $params, false, array(), true);
                }
            break;
            

            //ADD TASK
            case 'addTaskError':
                $this->view = $this->errorInfo("Nie udało się dodać zadania do projektu.");
            case 'addTask':
                $task = isset($_POST['task']) ? $_POST['task'] : array();
                $project = new Project((int)$params['pid']);
                if($project->getPid() < 1) {
                    $this->view .= '<center><b>Wybrany projekt nie istnieje.</b></center><br />';
                    $buttons = array();
                    $buttons[] = array('action'=>'manage','text'=>'Powrót');
                    $this->view .= Menu::createMenu($buttons, $params, false, array(), true);
                } else {
                    $this->view .= '<center>Dodawanie zadania dla projektu: <b>' . $project->name . '</b></center><br />';
                    $f = new Form($this->createLink($params), 'post');
                        $f->addSection("Dane zadania",'projectData');
                            $f->section()->add(new Input('Nazwa: ', 'text', "task[name]",isset($task['name']) ? $task['name'] : ''));
                            $textArea = new Textarea('task[description]', isset($task['description']) ? $task['description'] : '', '', 'width: 350px; height: 150px;', 20, 40);
                            $f->section()->add(new PureHtml('Opis:<br>' . $textArea->get()));
                            $f->section()->add(new Input('Priorytet:', 'text', 'task[priority]', isset($task['priority']) ? $task['priority'] : '','','','','priority'));
                            $f->section()->add(new Input('Data rozpoczęcia:', 'text', 'task[dateStart]', isset($task['dateStart']) ? $task['dateStart'] : date("Y-m-d"),'','','','dateStart'));
                            $f->section()->add(new Input('Data zakończenia:', 'text',  'task[dateEnd]', isset($task['dateEnd']) ? $task['dateEnd'] : date("Y-m-d"),'','','','dateEnd'));
                        $f->addButton(new Input('', 'submit', 'sbmt', 'Dodaj'));
                        $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '&action=manageProject&pid=' . $project->getPid() . '\'"'));
                    $this->view .= $f->get();
                }
            break;
            case 'addTaskSuccess':
                $buttons = array();
                $buttons[] = array('action'=>'manageProject','text'=>'Powrót');
                $task = $params['task'];
                $project = new Project((int)$task->getPid());
                unset($params['task']);
                $this->view = 'Zadanie "' . $task->name . '" zostało dodane do projektu ' . $project->name;
                $this->view .= Menu::createMenu($buttons, $params);
            break;
        
            case 'addUserError':
                $this->view .= $this->errorInfo("Nie udało się przypisać użytkownika do zadania.");
            case 'addUser':
                $task = new ProjectTask((int)$params['ptid']);
                $this->view .= '<center><b>Przypisywanie użytkownika do zadania: ' . $task->name . '</b></center>';
                $f = new Form($this->createLink($params), 'post');
                    $f->addSection("Wybór użytkownika",'addUserForm');
                        $f->section()->add(new PureHtml('<span>Wpisz przynajmniej 3 znaki imienia, nazwiska lub loginu użytkownika</span>'));
                        $f->section()->add(new Input('','text','user','','','','','user'));
                        $f->section()->add(new Input('','hidden','uid','0','','','','uid'));
                    $f->addButton(new Input('','submit','sbmt','Przypisz','','','','assign'));
                    $params['action'] = 'manageProject';
                    unset($params['ptid']);
                    $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'' . $this->createLink($params) . '\'"'));
                $this->view .= $f->get();
            break;
            case 'addUserSuccess':
                $params['action'] = 'manageProject';
                unset($params['ptid']);
                $this->view = '<script type="text/javascript" language="javascript">' . "
                                    location.href = '" . $this->createLink($params) . "';
                                </script>";
            break;
//ADD TASK
            case 'updateTaskError':
                $this->view = $this->errorInfo("Nie udało się zaktualizować danych zadania.");
            case 'updateTask':
                $task = isset($_POST['task']) ? $_POST['task'] : array();
                $taskObj = new ProjectTask((int)$params['ptid']);
                $project = new Project((int)$params['pid']);
                if($project->getPid() < 1) {
                    $this->view .= '<center><b>Wybrany projekt nie istnieje.</b></center><br />';
                    $buttons = array();
                    $buttons[] = array('action'=>'manage','text'=>'Powrót');
                    $this->view .= Menu::createMenu($buttons, $params, false, array(), true);
                } else {
                    $this->view .= '<center>Aktualizacja zadania z projektu: <b>' . $project->name . '</b></center><br />';
                    $f = new Form($this->createLink($params), 'post');
                        $f->addSection("Dane zadania",'projectData');
                            $f->section()->add(new Input('Nazwa: ', 'text', "task[name]",isset($task['name']) ? $task['name'] : $taskObj->name));
                            $textArea = new Textarea('task[description]', isset($task['description']) ? $task['description'] : $taskObj->description, '', 'width: 350px; height: 150px;', 20, 40);
                            $f->section()->add(new PureHtml('Opis:<br>' . $textArea->get()));
                            $f->section()->add(new Input('Priorytet:', 'text', 'task[priority]', isset($task['priority']) ? $task['priority'] : $taskObj->priority,'','','','priority'));
                            $f->section()->add(new Input('Data rozpoczęcia:', 'text', 'task[dateStart]', isset($task['dateStart']) ? $task['dateStart'] : $taskObj->dateStart,'','','','dateStart'));
                            $f->section()->add(new Input('Data zakończenia:', 'text',  'task[dateEnd]', isset($task['dateEnd']) ? $task['dateEnd'] : $taskObj->dateEnd,'','','','dateEnd'));
                        $f->addButton(new Input('', 'submit', 'sbmt', 'Aktualizuj'));
                        $f->addButton(new Input('','button','cancel','Anuluj','','','onClick="location.href=\'index.php?module=' . $params['module'] . '&action=manageProject&pid=' . $project->getPid() . '\'"'));
                    $this->view .= $f->get();
                }
            break;
            case 'updateTaskSuccess':
                $params['action'] = 'manageProject';
                unset($params['ptid']);
                unset($params['task']);
                $this->view = '<script type="text/javascript" language="javascript">' . "
                                    location.href = '" . $this->createLink($params) . "';
                                </script>";
            break;
        }
    }
}

?>
